<?
/***********************************************************/
/* Comom Form, implements Form Fields
/* by Pablo Dall'Oglio 2001-2006
/* revisado by Helder Mauricio Gomes Ferreira 04/2008
/***********************************************************/
class CommonForm
{
    /***********************************************************/
    /* Builds the Form Entries array
    /***********************************************************/
    function BuildFormEntries($Config, $CheckVars)
    {
        $i = 0;
        $count = count($CheckVars);
        foreach ($CheckVars as $CheckVar)
        {
            $Content     = $CheckVar[0];
            $Description = $CheckVar[1];
            $IsPassword  = $CheckVar[2];
            $Kind        = $CheckVar[3];
            $Editable    = $CheckVar[4];
            $Tooltip     = $CheckVar[5];

            $box = &new GtkHBox(false, 3);
            if (($i < ($count/2)) or ($count <= 2) or ($this->SimpleColumn))
            {
                $this->vbox->pack_start($box, false, false);
            }
            else
            {
                $this->vbox2->pack_start($box, false, false);
            }
            
            if (is_array($Kind))
            {
                $this->InputEntries[$Config][$i] = &new GtkCombo();
                $box->pack_start(new Box($this->InputEntries[$Config][$i], $Description), false, false, 10);
                $this->InputEntries[$Config][$i]->set_popdown_strings($Kind);
                $entry =$this->InputEntries[$Config][$i]->entry;
                $entry->set_text($Content);
                $entry->set_editable($Editable);
                $entry->set_size_request(126,20);
            }
            elseif (is_object($Kind))
            {
                $this->InputEntries[$Config][$i] = $Kind;
                $this->InputEntries[$Config][$i]->set_size_request(126, 20);
                $this->InputEntries[$Config][$i]->set_editable($Editable);
                $this->InputEntries[$Config][$i]->set_text($Content);

                $box->pack_start($this->InputEntries[$Config][$i]->widget, false, false, 10);
                $this->InputEntries[$Config][$i]->show();
                if ($IsPassword)
                {
                    $this->InputEntries[$Config][$i]->set_visibility(false);
                }
            }
            else if ($Kind=='spin')
            {
                $this->InputEntries[$Config][$i] = &new GtkSpinButton(new GtkAdjustment((double)$Content, 0, 999, 1, 10, 10));
                $this->InputEntries[$Config][$i]->set_position(0);
                $this->InputEntries[$Config][$i]->set_size_request(88, 20);
                $box->pack_start(new Box($this->InputEntries[$Config][$i], $Description), false, false, 10);
                if ($IsPassword)
                {
                    $this->InputEntries[$Config][$i]->set_visibility(false);
                }
                $this->InputEntries[$Config][$i]->show();
            }
            else if ($Kind=='colors')
            {                
                $this->InputEntries[$Config][$i] = &new ColorButton($Description);
                $this->InputEntries[$Config][$i]->set_usize(126, 20);
                $this->InputEntries[$Config][$i]->set_text($Content);

                $box->pack_start($this->InputEntries[$Config][$i]->widget, false, false, 10);
                $this->InputEntries[$Config][$i]->show();
            }
            else if ($Kind=='gtkfont')
            {
                $this->InputEntries[$Config][$i] = &new GtkFontBox($Description, $Content);
                $this->InputEntries[$Config][$i]->set_usize(126, 20);
                $this->InputEntries[$Config][$i]->set_editable($Editable);
                $this->InputEntries[$Config][$i]->set_text($Content);

                $box->pack_start($this->InputEntries[$Config][$i]->widget, false, false, 10);
                $this->InputEntries[$Config][$i]->show();
                if ($IsPassword)
                {
                    $this->InputEntries[$Config][$i]->set_visibility(false);
                }
            }
            else if (($Kind=='fonts') or ($Kind=='fonts2'))
            {
                $this->InputEntries[$Config][$i] = &new FontBox($Description, $Kind=='fonts2');
                $this->InputEntries[$Config][$i]->set_size_request(126, 20);
                $this->InputEntries[$Config][$i]->set_editable($Editable);
                $this->InputEntries[$Config][$i]->set_text($Content);

                $box->pack_start($this->InputEntries[$Config][$i]->widget, false, false, 10);
                $this->InputEntries[$Config][$i]->show();
                if ($IsPassword)
                {
                    $this->InputEntries[$Config][$i]->set_visibility(false);
                }
            }
            else if (($Kind=='files') or ($Kind=='files2'))
            {   //files returne um arquivo de figura 
                //file2 returne um diretorio do projeto
                $this->InputEntries[$Config][$i] = &new FileBox($Description, $Kind=='files2');
                $this->InputEntries[$Config][$i]->set_usize(126, 20);
                $this->InputEntries[$Config][$i]->set_editable($Editable);
                $this->InputEntries[$Config][$i]->set_text($Content);

                $box->pack_start($this->InputEntries[$Config][$i]->widget, false, false, 10);
                $this->InputEntries[$Config][$i]->show();
                if ($IsPassword)
                {
                    $this->InputEntries[$Config][$i]->set_visibility(false);
                }
            }
            else if ($Kind=='CheckButton')
            {
                
                $this->InputEntries[$Config][$i] = &new GtkCheckButton(_a('Yes'));
                $this->InputEntries[$Config][$i]->set_active($Content);
                $box->pack_start(new Box($this->InputEntries[$Config][$i], $Description), false, false, 10);
                $this->InputEntries[$Config][$i]->show();
            }
            else
            {
                $this->InputEntries[$Config][$i] = &new GtkEntry();
                $this->InputEntries[$Config][$i]->set_editable($Editable);
                $this->InputEntries[$Config][$i]->set_position(0);
                $box->pack_start(new EntryBox($this->InputEntries[$Config][$i], 100, $Description, $Content), false, false, 10);
                if ($IsPassword)
                $this->InputEntries[$Config][$i]->set_visibility(false);
                $this->InputEntries[$Config][$i]->show();
                $this->InputEntries[$Config][$i]->set_size_request(140,20);
            }

            if ($Kind == null)
                $Kind = 'Entry';

            if (is_array($Kind))
                $Kind = 'Combo';

            if ($Tooltip)
            {
                $this->tooltips->set_tip($this->InputEntries[$Config][$i], $Tooltip);
            }
            
            $this->InputKinds[$i] = $Kind;
            $i ++;
        }
        return $this->InputEntries;
    }

    /***********************************************************/
    /* KeyTest Method
    /***********************************************************/
    function KeyTest($obj, $window)
    {
        if ($obj->keyval == K_ESC)
        {
            $window->hide();
        }
    }

    /***********************************************************/
    /* Returns the Form Values from a specific sheet
    /***********************************************************/
    function GetFormValues($sheet)
    {
        if ($this->CheckVars[$sheet])
        {
            $count = count($this->CheckVars[$sheet]);
        }
        else
        {
            $count = count($this->Plus);
        }

        for ($n=0; $n< $count; $n++)
        {
            if ($this->InputKinds[$n] == 'Combo')
            {
                $entry = $this->InputEntries[$sheet][$n]->entry;
                $vetor[$n] = $entry->get_text();
            }
            else if ($this->InputKinds[$n] == 'spin')
            {
                $vetor[$n] = $this->InputEntries[$sheet][$n]->get_value_as_int();
            }
            else if ($this->InputKinds[$n] == 'CheckButton')
            {
                $vetor[$n] = $this->InputEntries[$sheet][$n]->get_active();
            }
            else
            {
                $vetor[$n] = $this->InputEntries[$sheet][$n]->get_text();
            }
        }
        return $vetor;
    }

    /***********************************************************/
    /* Returns the Current Values
    /***********************************************************/
    function GetVars()
    {
        $current = $this->Current();
        $vetor[$current] = $this->GetFormValues($current);
        return $vetor;
    }

    /***********************************************************/
    /* Returns all the Values
    /***********************************************************/
    function GetAllVars()
    {
        if ($this->CheckVars)
        {
            foreach ($this->CheckVars as $CheckVar => $content)
            {
                $count = count($CheckVar);
                $vetor[$CheckVar] = $this->GetFormValues($CheckVar);
            }
        }
        return $vetor;
    }
}
?>